//数据
import axios from "@/utils/http";
const state = {
  // 点击查看传送数据
  sendData: {},
  // 获取单据默认数据
  billData: {},
  billTotal: 1,
  // 根据当前编号查看采购单详情
  orderData: [],
  orderTotal: 1,
  // 审核状态下拉框
  auditData: [],
  // 供应商下拉框
  supplierData: [],
  // 处方总金额
  billPre: 1,
  // 批发总金额
  billTrade: 1,
};

const getters = {
  getBillId(state: any) {
    let arr = state.sendData.id;
    return arr;
  },
  // 获取单据默认数据
  getBillData(state: any) {
    let arr = state.billData;
    return arr;
  },
  // 根据当前编号查看采购单详情
  getOrderData(state: any) {
    let arr = state.orderData;
    return arr;
  },
  // 获取status的值判断
  getSendData(state: any) {
    let arr = state.sendData.status;
    return arr;
  },
  // 待修改
  // 审核状态下拉框
  getAuditData(state: any) {
    let arr = <any>[];
    state.auditData.forEach((item: any) => {
      arr.push(item);
    });
    console.log(arr);
    return arr;
  },
  // 待修改
  // 供应商下拉框
  getSupplierData2(state: any) {
    let arr = <any>[];
    state.supplierData.forEach((item: any) => {
      if (item) arr.push(item);
    });
    return arr;
  },
};

const actions = {};

const mutations = {
  // 查看按钮跳转
  sendData(state: any, data: any) {
    state.sendData = data;
    console.log("点击传送数据", data.status);
  },
  // 获取药品总库存表格默认数据
  setBillData(state: any, page: any) {
    axios({
      url: "/medicine/process/findAllBill",
      method: "get",
      params: {
        currentPage: page.currentPage,
        pageSize: page.pageSize,
        status: page.status, // 审核状态(0,1)
        supName: page.supName, //供应商
      },
    })
      .then((res: any) => {
        console.log("默认表格数2", res.data);
        state.billData = res.data.data;
        state.billTotal = res.data.count;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 根据id查询单据(制单号)
  setQueryData(state: any, page: any) {
    axios({
      url: "/medicine/process/findBillById",
      method: "get",
      params: {
        id: page.id,
      },
    })
      .then((res: any) => {
        console.log("根据制单号查询出来的数据", res.data.data[0].allPre);
        state.billData = res.data.data;
        state.billTotal = res.data.data.length;
        state.billPre = res.data.data[0].allPre;
        state.billTrade = res.data.data[0].allTrade;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 根据当前编号查看采购单详情(查看按钮)
  setOrderData(state: any, page: any) {
    axios({
      url: "/medicine/process/findOrderByBillId",
      method: "get",
      params: {
        currentPage: page.currentPage,
        pageSize: page.pageSize,
        billId: page.billId,
      },
    })
      .then((res: any) => {
        console.log("查看按钮查询到的数据", res);

        state.orderData = res.data.data;
        state.orderTotal = res.data.count;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 待修改
  // 审核状态下拉框
  setAuditList(state: any) {
    axios({
      url: "/medicine/process/findAllStatus",
      method: "get",
      params: {},
    })
      .then((res: any) => {
        console.log("审核状态下拉框", res.data.data);
        state.auditData = res.data.data;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 待修改
  // 供应商下拉框
  setSupplierList(state: any) {
    axios({
      url: "/medicine/process/findAllSupName",
      method: "get",
      params: {},
    })
      .then((res: any) => {
        console.log("供应商下拉框", res.data.data);
        state.supplierData = res.data.data;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  // 根据制单号删除订单
  setDeleteList(state: any, page: any) {
    axios({
      url: "/medicine/process/deleteById",
      method: "get",
      params: {
        id: page.id,
      },
    })
      .then((res: any) => {
        console.log("删除订单", res.code);
        state.billData = res.data.data;
      })
      .catch((err) => {
        console.log(err);
      });
  },

  // 审核按钮
  setAuditFn(state: any, page: any) {
    axios({
      url: "/medicine/process/check",
      method: "post",
      params: {
        id: page.id,
      },
    })
      .then((res: any) => {
        console.log("点击审核按钮返回的数据", res);
        // state.billData = res.data.data;
      })
      .catch((err) => {
        console.log(err);
      });
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
